package com.mr.view;

import javax.imageio.ImageIO;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

//滚动背景
public class BackgroundImage {
    public BufferedImage image;//背景图片
    private BufferedImage image1, image2;//滚动的两个图片
    private Graphics2D g;//背景图片的绘图对象
    public int x1, x2;//两个滚动图片的坐标
    public static final int SPEED = 4;//滚动速度

    public  BackgroundImage(){
        try {
            image1 = ImageIO.read(new File("image/1.jpg"));
            image2 = ImageIO.read(new File("image/2.jpg"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        //主图片采用宽1000高500的彩色图片
        image = new BufferedImage(1000,500,BufferedImage.TYPE_INT_BGR);
        g = image.createGraphics();//获取主图片绘图对象
        x1 = 0;//第一幅图片初始横坐标为0
        x2 = 1000;//第二幅图片初始横坐标为1000
        g.drawImage(image1,x1,0,null);
    }
    public void roll(){
        x1 -= SPEED;//第一幅图片左移
        x2 -= SPEED;//第二幅图片左移
        if (x1 <= -1000){//如果第一幅图片移出屏幕
            x1 = 1000;//回到屏幕右侧
        }
        if (x2 <= -1000){//如果第二幅图片移出屏幕
            x2 = 1000;//回到屏幕右侧
        }
        g.drawImage(image1,x1,0,null);//在主图片中绘制两幅图片
        g.drawImage(image2,x2,0,null);
    }
}